package com.bfs.service.hystrix.collaspers.observable;/**
 * @Deacription TODO
 * @Author baohuajie
 * @Date 2020/12/15 15:27
 * @Version 1.0
 **/

import cfk.pojo.order.CustQueryReq;
import cfk.pojo.order.CustQueryResp;
import com.bfs.service.hystrix.collaspers.QueryService;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixObservableCommand;
import rx.Observable;
import rx.Subscriber;

import java.util.List;
import java.util.UUID;

/**
 *@Deacription TODO
 *@Author baohuajie
 *@Date 2020/12/15 15:27
 *@Version 1.0
 **/
public class Obserable extends HystrixObservableCommand {

    private QueryService queryService;

    public Obserable(QueryService queryService) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("queryUserInfoGroupKey"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("queryUserInfoKey"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
                        .withExecutionIsolationThreadInterruptOnTimeout(true)
                        .withExecutionTimeoutInMilliseconds(5000)));
        this.queryService = queryService;
    }

    @Override
    public Observable construct() {

        Observable<Object> observable = Observable.create(new Observable.OnSubscribe<Object>() {

            @Override
            public void call(Subscriber<? super Object> subscriber) {
                CustQueryReq reqData = new CustQueryReq();
                reqData.setCustId(UUID.randomUUID().toString());
                List<CustQueryResp> custQueryResps = queryService.hystrixObservable(reqData);
                subscriber.onNext(custQueryResps);
                String single = queryService.querySingle(UUID.randomUUID().toString());
                subscriber.onNext(single);

                subscriber.onCompleted();
            }
        });

        return observable;
    }
}
